package com.llf.Config;



import io.swagger.models.Path;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //配置Swagger的bean实例
    @Bean
    public Docket docket(Environment environment){

        //设置Swagger要显示的环境
        Profiles profiles= Profiles.of("dev");


        //判断运行的环境是不是想要设置的Swagger环境
        boolean flag = environment.acceptsProfiles(profiles);

        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否启动配置 默认为true
                .enable(flag)
                .groupName("飞哥")
                .select()
                /*
                配置扫描接口的方式
                 1、扫描方法上注解为GetMapping的请求
                 .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                 2、扫描类上为Controller的所有类中的请求
                .apis(RequestHandlerSelectors.withClassAnnotation(Controller.class))
                3、什么也不扫描
                .apis(RequestHandlerSelectors.none())
                4、扫描所有请求
                 .apis(RequestHandlerSelectors.any())
                 */
                // 只扫描com.llf.controller包下的请求
                .apis(RequestHandlerSelectors.basePackage("com.llf.Blog"))
                // paths  过滤请求
                /*
                .paths(PathSelectors.any() 扫描所有请求
                .paths(PathSelectors.none（）什么都不扫描
                .paths(PathSelectors.ant("/llf/**"))    只扫描/llf/**路径下的所有请求

                可配可不配
                 */
                // .paths(PathSelectors.ant("/llf/**"))
                .build();
    }


    //配置swagger信息 apiInfo
    private ApiInfo apiInfo(){
        //配置作者信息
        Contact DEFAULT_CONTACT
                = new Contact("llf", "www.baidu.com", "1471305491@QQ.com");
        return   new ApiInfo("飞哥的SwaggerAPI文档",
                "我要学习啦",
                "V1.0",
                "http://localhost:8080/",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }

}
